﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualRink.Library;

namespace VirtualRink.DevHelper
{
	public class ResetTestDataOperation
	{
		IRepository repository;
		VirtualRinkModelDataContext context;

		public ResetTestDataOperation()
		{
			this.context = new VirtualRinkModelDataContext();
			this.repository = new LinqRepository(context);
		}

		public void Execute()
		{
			NameGenerator.Initialize();

			this.context.Contracts.DeleteAllOnSubmit(this.context.Contracts);
			this.context.Players.DeleteAllOnSubmit(this.context.Players);
			this.context.Teams.DeleteAllOnSubmit(this.context.Teams);
			this.context.Divisions.DeleteAllOnSubmit(this.context.Divisions);
			this.context.Conferences.DeleteAllOnSubmit(this.context.Conferences);
			this.context.Leagues.DeleteAllOnSubmit(this.context.Leagues);
			this.context.SubmitChanges();

			League league = this.repository.CreateLeague("Test League");
			Conference west = this.repository.CreateConference("West", league);
			Conference east = this.repository.CreateConference("East", league);

			Division northwest = this.repository.CreateDivision("Northwest", west);
			Division southwest = this.repository.CreateDivision("Southwest", west);
			Division northeast = this.repository.CreateDivision("Northeast", east);
			Division southeast = this.repository.CreateDivision("Southeast", east);

			int count = 0;

			for (int i = 0; i < 20; i++)
			{
				this.repository.CreatePlayer(league, NameGenerator.GetFirstName(),
					NameGenerator.GetLastName(), count, "L", "C");
				count++;
				this.repository.CreatePlayer(league, NameGenerator.GetFirstName(),
					NameGenerator.GetLastName(), count, "L", "LW");
				count++;
				this.repository.CreatePlayer(league, NameGenerator.GetFirstName(),
					NameGenerator.GetLastName(), count, "L", "RW");
				count++;
				this.repository.CreatePlayer(league, NameGenerator.GetFirstName(),
					NameGenerator.GetLastName(), count, "L", "D");
				count++;
				this.repository.CreatePlayer(league, NameGenerator.GetFirstName(),
					NameGenerator.GetLastName(), count, "L", "D");
				count++;
				this.repository.CreatePlayer(league, NameGenerator.GetFirstName(),
					NameGenerator.GetLastName(), count, "L", "G");
				count++;
			}

			Team team1 = this.repository.CreateTeam("Blizzard", "Moose Jaw", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
				northwest);
			Team team2 = this.repository.CreateTeam("Ions", "Winnepeg", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
				northwest);
			Team team3 = this.repository.CreateTeam("Bakers", "Seattle", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
				northwest);
			Team team4 = this.repository.CreateTeam("Cowboys", "Cheyenne", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
				northwest);

			Team team5 = this.repository.CreateTeam("Berserkers", "Erie", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	northeast);
			Team team6 = this.repository.CreateTeam("Carpenters", "Syracuse", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	northeast);
			Team team7 = this.repository.CreateTeam("Astronauts", "Nova Scocia", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	northeast);
			Team team8 = this.repository.CreateTeam("Farmers", "Deleware", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	northeast);

			Team team9 = this.repository.CreateTeam("Surfers", "Los Angeles", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	southwest);
			Team team10 = this.repository.CreateTeam("Coders", "Portland", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	southwest);
			Team team11 = this.repository.CreateTeam("Juggernauts", "Las Vegas", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	southwest);
			Team team12 = this.repository.CreateTeam("Rampage", "San Francisco", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	southwest);

			Team team13 = this.repository.CreateTeam("Swordsmen", "Mississippi", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
southeast);
			Team team14 = this.repository.CreateTeam("Bludgeoneers", "Kentucky", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	southeast);
			Team team15 = this.repository.CreateTeam("Psychiatrists", "Nashville", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	southeast);
			Team team16 = this.repository.CreateTeam("Kittens", "Virginia", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
	southeast);

			DraftPlayers(league, team1);
			DraftPlayers(league, team2);
			DraftPlayers(league, team3);
			DraftPlayers(league, team4);
			DraftPlayers(league, team5);
			DraftPlayers(league, team6);
			DraftPlayers(league, team7);
			DraftPlayers(league, team8);
			DraftPlayers(league, team9);
			DraftPlayers(league, team10);
			DraftPlayers(league, team11);
			DraftPlayers(league, team12);
			DraftPlayers(league, team13);
			DraftPlayers(league, team14);
			DraftPlayers(league, team15);
			DraftPlayers(league, team16);

			//generate a little contract history...



			DoTrade(team1, team2, "C");
			DoTrade(team6, team1, "C");
			DoTrade(team4, team9, "LW");
			DoTrade(team13, team7, "D");
			DoTrade(team15, team1, "G");
			DoTrade(team9, team1, "C");
			DoTrade(team9, team13, "C");
			DoTrade(team16, team15, "RW");

		}

		void DoTrade(Team team1, Team team2, string position)
		{
			this.repository.TradePlayers(team1, team1.GetPlayerByPosition(position),
							team2, team2.GetPlayerByPosition(position));
		}

		void DraftPlayers(League league, Team team)
		{
			this.repository.DraftPlayer(team,
				this.GetAvaialblePlayer(league, "LW"));
			this.repository.DraftPlayer(team,
				this.GetAvaialblePlayer(league, "C"));
			this.repository.DraftPlayer(team,
				this.GetAvaialblePlayer(league, "RW"));
			this.repository.DraftPlayer(team,
				this.GetAvaialblePlayer(league, "D"));
			this.repository.DraftPlayer(team,
				this.GetAvaialblePlayer(league, "D"));
			this.repository.DraftPlayer(team,
				this.GetAvaialblePlayer(league, "G"));
		}


		Player GetAvaialblePlayer(League league, string position)
		{
			IEnumerable<Player> players = this.repository.GetAvailablePlayers(league, position);
			Player player = players.FirstOrDefault();
			return player;
		}
	}
}
